<template>
    <div class="wx_pay">
        <div class="header_view">
            <div class="header-container">
                <div class="titles">
                    <img src="https://anhuiyaoying.com/jiecunyun/logo.png" alt="">
                    <span>我的收银台</span>
                </div>
            </div>
        </div>
        <div class="container">
            <div class="top">
                <div class="message">
                    <div class="left">
                        <span class="txt1">捷存云会员</span>
                        <span class="txt2">收款方：安徽曜影网络科技有限公司</span>
                    </div>
                    <div class="right">
                        <span class="money">{{ amount }}</span>
                        <span class="unit">元</span>
                    </div>
                </div>
            </div>
            <div class="bottom">
                <div class="codes">
                    <div class="logo">
                        <img src="https://anhuiyaoying.com/webs/wxpay.png" alt="">
                        <span>微信支付</span>
                    </div>
                    <div class="qrcode">
                        <img :src="qrcode_url" alt="">
                    </div>
                    <p class="qrcode-text">请打开手机微信</p>
                    <p class="qrcode-text">扫一扫完成支付</p>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import QRCode from 'qrcode';
export default {
    name: 'wxPay',
    data() {
        return {
            amount: 0,
            qrUrl: '',  //二维码对应的url
            qrcode_url: '',  //生成的二维码图片
        }
    },
    created() {
        this.amount = (parseInt(this.getQueryParameter('amount'))/100).toFixed(2)
        this.qrUrl = this.getQueryParameter('code_url')
        this.generateQRCode()
    },
    methods: {
        // 解析URL参数
        getQueryParameter(key) {
            let url = decodeURIComponent(location.href)
            // 分割 URL，获取 # 之后的部分
            const fragment = url.split('#')[1];
            if (!fragment) return null;

            // 查找第一个问号的位置
            const queryStartIndex = fragment.indexOf('?');
             // 如果没有问号，返回 null
            if (queryStartIndex === -1) return null;

            // 提取查询字符串，从问号开始到字符串结束
            const queryString = fragment.substring(queryStartIndex + 1);
            
            // 使用 URLSearchParams 获取查询参数
            const params = new URLSearchParams(queryString);
            
            // 返回指定键的值
            return params.get(key);
        },
        // 生成二维码
        generateQRCode() {
            let that = this
            try {
                QRCode.toDataURL(this.qrUrl, function(error, url) {
                    if (error) {
                        console.error(error);
                    } else {
                        that.qrcode_url = url
                    }
                })
            } catch (error) {
                console.error('生成二维码失败:', error);
            }
        }
    }
}
</script>

<style lang="less" scoped>
	@import '../../style/mixin';
    .wx_pay {
        background: #eff0f1;
        height: 100%;
        .header_view {
            height: 60px;
            background: #fff;
            border-bottom: 1px solid #d9d9d9;
            .header-container {
                width: 60%;
                height: 100%;
                margin: 0 auto;
                .titles {
                    height: 100%;
                    float: left;
                    img {
                        display: block;
                        position: relative;
                        left: 0;
                        top: 10px;
                        float: left;
                        height: 40px;
                        width: 40px;
                        border-radius: 8px;
                    }
                    span {
                        font-size: 16px;
                        border-left: 1px solid #676d70;
                        color: #676d70;
                        height: 20px;
                        float: left;
                        margin-top: 15px;
                        margin-left: 10px;
                        padding-top: 10px;
                        padding-left: 10px;
                    }
                }
            }
        }
        .container {
            width: 60%;
            margin: 0 auto;
            overflow: hidden;
            .top {
                position: relative;
                margin-top: 15px;
                padding: 16px 23px;
                border-bottom: 3px solid @blue;
                background: #eff0f1;
                color: #000;
                .message {
                    height: 22px;
                    display: flex;
                    align-items: center;
                    justify-content: space-between;
                    .left {
                        display: inline-flex;
                        align-items: center;
                        font-size: 14px;
                        .txt1 {
                            font-weight: 700;
                        }
                        .txt2 {
                            margin-left: 20px;
                            color: #000;
                        }
                    }
                    .right {
                        .money {
                            color: #F5242D;
                            font-size: 22px;
                            font-weight: bold;
                        }
                        .unit {
                            color: #000;
                        }
                    }
                }
            }
            .bottom {
                text-align: center;
                background: #fff;
                height: 420px;
                .codes {
                    width: 280px;
                    display: flex;
                    flex-direction: column;
                    justify-content: center;
                    align-items: center;
                    margin: 0 auto;
                    .logo {
                        width: 300px;
                        height: 60px;
                        display: flex;
                        align-items: center;
                        justify-content: center;
                        margin-top: 20px;
                        img {
                            width: 43px;
                            height: 36px;
                            margin-right: 8px;
                        }
                        span {
                            font-size: 19px;
                            color: #666;
                        }
                    }
                    .qrcode {
                        img {
                            width: 210px;
                            height: 210px;
                        }
                    }
                    .qrcode-text {
                        font-size: 14px;
                        margin-bottom: 10px;
                        color: #999999;
                        display: block;
                        text-align: center;
                    }
                }
            }
        }
    }
</style>